﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SagiShopLibrary.BusinessObjects;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.DXErrorProvider;

namespace SagiShop.Modules.ModuleQuanLy
{
    public partial class ScreenTTChiNhanh : DevExpress.XtraEditors.XtraForm
    {
        private int rowid;
        public ScreenTTChiNhanh()
        {
            InitializeComponent();
            this.AddEvent();
            this.SetStatusButtons();
            this.CreateColumnGridview();
            this.LoadRecords();
            this.LoadLabel();       
        }
        private void AddEvent()
        {
            btnDieuChinh.Click += new EventHandler(btnDieuChinh_Click);
            btnThemMoi.Click += new EventHandler(btnThemMoi_Click);
            btnLuu.Click += new EventHandler(btnLuu_Click);
            btnHuy.Click += new EventHandler(btnHuy_Click);
            txtTenChiNhanh.Validated += delegate(object sender, EventArgs e)
                                        {
                                            bool status = dxValidationProviderTTCN.Validate();
                                            status = true;
                                        };
            txtTenChiNhanh.Validating += delegate(object sender, CancelEventArgs e)
                                        {
                                            dxValidationProviderTTCN.Validate();
                                        };
            gridViewTTChiNhanh.RowClick += new DevExpress.XtraGrid.Views.Grid.RowClickEventHandler(gridViewChiNhanh_RowClick);
            var notEmptyValidationRule = new ConditionValidationRule()
            {
                ConditionOperator = ConditionOperator.IsNotBlank,
                ErrorText = "Bạn Chưa Nhập Tên Chi Nhánh"
            };
            dxValidationProviderTTCN.SetValidationRule(txtTenChiNhanh, notEmptyValidationRule);
        }


        private void gridViewChiNhanh_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            bool status = dxValidationProviderTTCN.Validate();
            status = true;
            if (e.Clicks > 0)
            {
                btnThemMoi.Enabled = false;
                btnDieuChinh.Enabled = true;
                txtTenChiNhanh.Enabled = false;
                txtDiaChiChiNhanh.Enabled = false;
                txtSoDienThoai.Enabled = false;
                txtEmailChiNhanh.Enabled = false;
                rowid = (Int32)gridViewTTChiNhanh.GetRowCellValue(e.RowHandle, "Mã Chi Nhánh");
                labelMaChiNhanh.Text = Convert.ToString(gridViewTTChiNhanh.GetRowCellValue(e.RowHandle, "Mã Chi Nhánh"));
                labelTenChiNhanh.Text = Convert.ToString(gridViewTTChiNhanh.GetRowCellValue(e.RowHandle, "Tên Chi Nhánh"));
                labelDiaChiChiNhanh.Text = Convert.ToString(gridViewTTChiNhanh.GetRowCellValue(e.RowHandle, "Địa Chỉ"));
                labelSDTChiNhanh.Text = Convert.ToString(gridViewTTChiNhanh.GetRowCellValue(e.RowHandle, "Số Điện Thoại"));
                labelEmailChiNhanh.Text = Convert.ToString(gridViewTTChiNhanh.GetRowCellValue(e.RowHandle, "Email"));
                txtTenChiNhanh.Text = gridViewTTChiNhanh.GetRowCellValue(e.RowHandle, "Tên Chi Nhánh").ToString();
                txtDiaChiChiNhanh.Text = gridViewTTChiNhanh.GetRowCellValue(e.RowHandle, "Địa Chỉ").ToString();
                txtSoDienThoai.Text = gridViewTTChiNhanh.GetRowCellValue(e.RowHandle, "Số Điện Thoại").ToString();
                txtEmailChiNhanh.Text = gridViewTTChiNhanh.GetRowCellValue(e.RowHandle, "Email").ToString();
            }
        }
        
        #region su kien button
        private void btnLuu_Click(object sender, EventArgs e)
        {
            bool status = dxValidationProviderTTCN.Validate();                                            
            try
            {
                if (status)
                {
                    var tenChiNhanh = txtTenChiNhanh.Text.Trim();
                    var diaChiChiNhanh = txtDiaChiChiNhanh.Text.Trim();
                    var sdtChiNhanh = txtSoDienThoai.Text.Trim();
                    var emailChiNhanh = txtEmailChiNhanh.Text.Trim();
                    try
                    {
                        BOChinhanh boCH = new BOChinhanh() { IdChinhanh = rowid, TenChinhanh = tenChiNhanh, DiachiChinhanh = diaChiChiNhanh, SdtChinhanh = sdtChiNhanh, EmailChinhanh = emailChiNhanh };
                        boCH.Update();
                        btnDieuChinh.Enabled = false;
                        btnLuu.Enabled = false;
                        btnThemMoi.Enabled = true;
                        this.EmptyTextBox();
                        txtDiaChiChiNhanh.Focus();
                        this.LoadRecords();
                    }
                    catch (Exception)
                    {

                        XtraMessageBox.Show("Cập Nhật Không Thành Công");
                    }
                }
                else
                {
                    dxValidationProviderTTCN.Validate();
                    txtTenChiNhanh.Focus();
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            try
            {
                if (!String.IsNullOrEmpty(txtTenChiNhanh.Text))
                {
                    var tenChiNhanh = txtTenChiNhanh.Text.Trim();
                    var diaChiChiNhanh = txtDiaChiChiNhanh.Text.Trim();
                    var sdtChiNhanh = txtSoDienThoai.Text.Trim();
                    var emailChiNhanh = txtEmailChiNhanh.Text.Trim();
                    try
                    {
                        BOChinhanh boChiNhanh = new BOChinhanh() { TenChinhanh = tenChiNhanh, DiachiChinhanh = diaChiChiNhanh, SdtChinhanh = sdtChiNhanh, EmailChinhanh = emailChiNhanh, TtChinhanh = true };
                        boChiNhanh.SaveNew();
                        this.EmptyTextBox();
                        txtTenChiNhanh.Focus();
                        this.LoadRecords();
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
                else
                {
                    dxValidationProviderTTCN.Validate();
                    txtTenChiNhanh.Focus();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnDieuChinh_Click(object sender, EventArgs e)
        {
            btnThemMoi.Enabled = false;
            btnLuu.Enabled = true;
            txtTenChiNhanh.Enabled = true;
            txtDiaChiChiNhanh.Enabled = true;
            txtSoDienThoai.Enabled = true;
            txtEmailChiNhanh.Enabled = true;
        }
        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.EmptyTextBox();
            if (btnDieuChinh.Enabled)// tra ve true
            {
                txtTenChiNhanh.Enabled = true;
                txtDiaChiChiNhanh.Enabled = true;
                txtSoDienThoai.Enabled = true;
                txtEmailChiNhanh.Enabled = true;
                btnDieuChinh.Enabled = false;
                btnThemMoi.Enabled = true;
                btnLuu.Enabled = false;
                EmptyTextBox();
                txtDiaChiChiNhanh.Focus();
            }
        }
        private void SetStatusButtons()
        {
            btnThemMoi.Enabled = true;
            btnLuu.Enabled = false;
            btnDieuChinh.Enabled = false;
        }
        #endregion
        #region Su kien cho textbox

        private void EmptyTextBox()
        {
            txtTenChiNhanh.Text = string.Empty;
            txtDiaChiChiNhanh.Text = string.Empty;
            txtSoDienThoai.Text = string.Empty;
            txtEmailChiNhanh.Text = string.Empty;
        }

        #endregion
        #region Khoi Tao Database
        /// <summary>
        /// Load tren label
        /// </summary>
        public int LoadLabel() 
        {
            rowid = 1;
            BOChinhanh boCH = new BOChinhanh(rowid);
            labelMaChiNhanh.Text = Convert.ToString(boCH.IdChinhanh);
            labelTenChiNhanh.Text = boCH.TenChinhanh;
            labelDiaChiChiNhanh.Text = boCH.DiachiChinhanh;
            labelSDTChiNhanh.Text = boCH.SdtChinhanh;
            labelEmailChiNhanh.Text = boCH.EmailChinhanh;
            return rowid;
            
        }
        /// <summary>
        /// tao du lieu cho row
        /// </summary>
        public void LoadRecords()
        {
            try
            {
                var table = new DataTable();
                table.Columns.Add("Mã Chi Nhánh", typeof(Int32));
                //table.Columns[0].ColumnMapping = MappingType.Hidden;
                table.Columns.Add("Tên Chi Nhánh", typeof(string));
                table.Columns.Add("Địa Chỉ", typeof(string));
                table.Columns.Add("Số Điện Thoại", typeof(string));
                table.Columns.Add("Email", typeof(string));
                var boTTCNList = BOChinhanh.ChinhanhCollection();
                foreach (var boTTCN in boTTCNList)
                {
                    table.Rows.Add(boTTCN.IdChinhanh, boTTCN.TenChinhanh, boTTCN.DiachiChinhanh, boTTCN.SdtChinhanh, boTTCN.EmailChinhanh);
                }

                this.bindingSourceTTCN.DataSource = table;
                this.gridControlTTChiNhanh.DataSource = this.bindingSourceTTCN;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Không Hiển Thị Được");
                return;
            }
        }
        /// <summary>
        /// tao title cho colum cua grid view
        /// </summary>
        private void CreateColumnGridview()
        {
            GridColumn colId = new GridColumn();
            colId.FieldName = colId.Caption = "Mã Chi Nhánh";
            colId.VisibleIndex = 0;
            gridViewTTChiNhanh.Columns.Add(colId);

            GridColumn colName = new GridColumn();
            colName.FieldName = colName.Caption = "Tên Chi Nhánh";
            colName.VisibleIndex = 1;
            gridViewTTChiNhanh.Columns.Add(colName);

            GridColumn colDiaChi = new GridColumn();
            colDiaChi.FieldName = colDiaChi.Caption = "Địa Chỉ";
            colDiaChi.VisibleIndex = 2;
            gridViewTTChiNhanh.Columns.Add(colDiaChi);

            GridColumn colSDT = new GridColumn();
            colSDT.FieldName = colSDT.Caption = "Số Điện Thoại";
            colSDT.VisibleIndex = 3;
            gridViewTTChiNhanh.Columns.Add(colSDT);

            GridColumn colEmail = new GridColumn();
            colEmail.FieldName = colEmail.Caption = "Email";
            colEmail.VisibleIndex = 4;
            gridViewTTChiNhanh.Columns.Add(colEmail);
        }
        #endregion
    }
}